package com.demo.websocket.config;

import com.demo.websocket.handler.WebSocketHandler;
import com.demo.websocket.interceptor.WebSocketInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

/**
 * @Author JiangMing Zhou
 * @Date 2023/4/20 16:26
 * @Version 1.8
 */
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {
    @Resource
    private WebSocketInterceptor webSocketInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(webSocketHandler(), "/websocket")
                .setAllowedOrigins("*")
                .addInterceptors(webSocketInterceptor);
    }

    @Bean
    public WebSocketHandler webSocketHandler() {
        return new WebSocketHandler();
    }
}
